package com.jyh.service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;

import com.jyh.config.BaseAction;
import com.jyh.config.quantity.NewRegister;
import com.jyh.db.dao.JurisdictionDao;
import com.jyh.db.entity.JyhManager;
import com.jyh.db.entity.JyhRightRole;
import com.jyh.util.JurVar;
import com.jyh.util.Utils;

@Service
public class SuperviseService extends BaseAction {
	@Autowired
	private JurisdictionDao jurisdictionDao;
	@Autowired
	private ButtonService buttonService;

	private static String rRole = "";
	private static String rInfo = "";
	private static List<String> topList;
	private static List<String> leftList;
	private static List<String> butList;
	private static int backOpen = 0;

	/**
	 * 获取管理员信息
	 * 
	 * @param request
	 * @param response
	 * @param session
	 * @return
	 */
	public ModelAndView getManager(HttpServletRequest request, HttpSession session) {
		ModelAndView mv = new ModelAndView("jurisdiction/manager");
		List<JyhManager> list = jurisdictionDao.getJyhManager();
//		List<Map> lists = (List<Map>) buttonService.getTableButton(1, "id,manger,roleId", jsonOutSimplify(list),session);
		mv.addObject("list", list);
		return mv;

	}

	/**
	 * 获取角色列表
	 * 
	 * @param request
	 * @param response
	 * @param session
	 * @return
	 */
	public ModelAndView getRoleList(HttpServletRequest request, HttpSession session) {
		ModelAndView mv = new ModelAndView("jurisdiction/roleList");
		List<JyhRightRole> list = jurisdictionDao.getRoleList();
		String button = (String) buttonService.getButton(1,session);
		mv.addObject("list", list);
		mv.addObject("firstButton", button);
		return mv;
	}

	/**
	 * 查询单一管理员权限
	 * 
	 * @param request
	 * @param session
	 * @return
	 */
	public ModelAndView getUpdateManager(HttpServletRequest request, HttpSession session) {
		ModelAndView mv = new ModelAndView("jurisdiction/managerUpdate");
		String id = request.getParameter("id");
		// 获取权限信息
		Map<String, Object> mapManager = jurisdictionDao.getJyhManager(id);
		List<NewRegister> nrList = jurisdictionDao.getVaultRightRole();
		mv.addObject("mapManager", mapManager);
		mv.addObject("nrList", nrList);
		mv.addObject("roleID", mapManager.get("role_id") + "");
		return mv;
	}

	/**
	 * 查询单一管理员权限
	 * 
	 * @param request
	 * @param session
	 * @return
	 */
	public ModelAndView getUpdateManager1(HttpServletRequest request, HttpSession session) {
		ModelAndView mv = new ModelAndView("jurisdiction/managerUpdate2");
		String id = session.getAttribute("userId")+"";
		// 获取权限信息
		Map<String, Object> mapManager = jurisdictionDao.getJyhManager(id);
		mv.addObject("mapManager", mapManager);
		mv.addObject("roleID", mapManager.get("role_id") + "");
		return mv;
	}

	/**
	 * 跳转管理员添加页面
	 * 
	 * @param request
	 * @param session
	 * @return
	 */
	public String getManagerU(HttpServletRequest request, HttpSession session) {
		String id = request.getParameter("id");
		String password = request.getParameter("password");
		String pwd = request.getParameter("pwd");
		String mobile = request.getParameter("mobile");
		if (!pwd.equals(password)) {
			password = Utils.string2MD5(password);
		}
//		String mobiles = request.getParameter("mobiles");
//		if (!mobile.equals(mobiles)) {
//			boolean boo = jurisdictionDao.getSelectNameManager(mobile);
//			if (boo) {
//				return "-1";
//			}
//		}
		String isFincing = request.getParameter("isFincing");
		String roleID = request.getParameter("roleID");
		int returnInt = jurisdictionDao.getUpdateJyhManager(id, password, isFincing, roleID, mobile);
		return returnInt + "";
	}

	/**
	 * 修改管理员权限
	 * 
	 * @param request
	 * @param session
	 * @return
	 */
	public String getManagerI(HttpServletRequest request, HttpSession session) {
		String userName = request.getParameter("userName");
		String password = request.getParameter("password");
		String mobile = request.getParameter("mobile");
		password = Utils.string2MD5(password);
		String isFincing = request.getParameter("isFincing");
		String roleID = request.getParameter("roleID");
		boolean boo = jurisdictionDao.getSelectNameManager(userName, mobile);
		if (boo) {
			return "-1";
		}
		int returnInt = jurisdictionDao.getAddJyhManager(userName, password, isFincing, roleID, mobile);
		return returnInt + "";
	}

	/**
	 * 新增管理员权限
	 * 
	 * @param request
	 * @param session
	 * @return
	 */
	public ModelAndView getAddManager(HttpServletRequest request, HttpSession session) {
		ModelAndView mv = new ModelAndView("jurisdiction/managerInsert");
		String id = request.getParameter("id");
		// 获取权限信息
		List<NewRegister> nrList = jurisdictionDao.getVaultRightRole();
		mv.addObject("nrList", nrList);
		return mv;

	}

	/**
	 * 已有权限数据查询
	 * 
	 * @return
	 */
	public String getJrData(String Nid) {
		String viewHtml = "";
		Map<String, Object> subMap = jurisdictionDao.getVaultRightRoleID(Nid);
		rRole = subMap.get("r_role").toString();
		rInfo = subMap.get("r_info").toString();
		topList = Arrays.asList(subMap.get("head_parameters").toString().split(","));
		leftList = Arrays.asList(subMap.get("left_parameters").toString().split(","));
		butList = Arrays.asList(subMap.get("center_parameters").toString().split(","));
		String views = "";
		views = showJurList(1);
		return views;
	}

	/**
	 * 权限类目分级罗列--一级
	 * 
	 * @return
	 */
	public String showJurList(int ong) {
		backOpen = ong;
		String viewHtml = "";
		String rname = JurVar.SUB_NAME;
		String rinfo = JurVar.SUB_RINFO;
		List<Map<String, Object>> mainMap = jurisdictionDao.getVaultNavigate();
		for (int i = 0; i < mainMap.size(); i++) {
			String MList = JurVar.MAIN_LIST;
			MList = MList.replace("[TID]", "TB_" + i);
			MList = MList.replace("[CID]", "navigateA");
			// {ID=1, nav_name=用户管理, sort=1, ico=null,
			// fun_parameters=/index/catalogue?cid=1, is_look=1}

			MList = MList.replace("[VUA]", mainMap.get(i).get("ID").toString());
			MList = MList.replace("[TIT]", mainMap.get(i).get("nav_name").toString());
			MList = MList.replace("[SUBS]", getSubList(mainMap.get(i).get("ID").toString(), i + ""));
			if (backOpen == 1 && topList.contains(mainMap.get(i).get("ID").toString())) {
				MList = MList.replace("[CHECK]", "checked");
			}
			if ("".equals(rname) && "".equals(rinfo)) {
				rname = rname.replace("[NAME]", "");
				rinfo = rinfo.replace("[RINFO]", "");
			} else {
				rname = rname.replace("[NAME]", rRole);
				rinfo = rinfo.replace("[RINFO]", rInfo);
			}
			MList = MList.replace("[CHECK]", "");
			viewHtml += MList;
		}
		return rname + rinfo + JurVar.MAIN_HEAD + viewHtml;

	}

	/**
	 * 权限类目分级罗列--二级
	 * 
	 * @return
	 */
	private String getSubList(String Nid, String Nos) {
		String viewHtml = "";
		List<Map<String, Object>> subMap = jurisdictionDao.getVaultMenu(Nid);
		for (int i = 0; i < subMap.size(); i++) {
			String MList = JurVar.SUB_LIST;
			MList = MList.replace("[TID]", "TB" + "_" + Nos + "_" + i);
			MList = MList.replace("[CID]", "navigateB");
			MList = MList.replace("[VUA]", subMap.get(i).get("ID").toString());
			MList = MList.replace("[TIT]", subMap.get(i).get("catalogue_name").toString());
			MList = MList.replace("[SUBS]", getThrList(subMap.get(i).get("ID").toString(),Nos+"_"+i));
			if (backOpen == 1 && leftList.contains(subMap.get(i).get("ID").toString())) {
				MList = MList.replace("[CHECK]", "checked");
			}
			MList = MList.replace("[CHECK]", "");
			viewHtml += MList;
		}
		return viewHtml;
	}
	
	/**
	 * 权限类目分级罗列--三级
	 * @return
	 */	
	private  String getThrList(String Nid, String Nos){
		String viewHtml = "";
		List<Map<String, Object>> subMap = jurisdictionDao.getVaultFunction(Nid);
		for(int i=0; i<subMap.size(); i++){
			String MList = JurVar.SUB_LIST;
			MList = MList.replace("[TID]", "TB"+"_"+Nos+"_"+i);
			MList = MList.replace("[CID]", "navigateC");
			MList = MList.replace("[VUA]", subMap.get(i).get("ID").toString());
			MList = MList.replace("[TIT]", subMap.get(i).get("fun_name").toString());
			MList = MList.replace("[SUBS]", "-");
			if(backOpen == 1 && butList.contains(subMap.get(i).get("ID").toString())){
				MList = MList.replace("[CHECK]", "checked");	
			}
			MList = MList.replace("[CHECK]", "");
			viewHtml += MList;
		}
		return viewHtml;		
	}

	/**
	 * 权限角色参数修改，新增
	 * 
	 * @param JA
	 *            一级参数
	 * @param JB
	 *            二级参数
	 * @param JC
	 *            三级参数
	 * @param ID
	 *            修改ID
	 */
	public String updateList(String NM, String Inf, String[] JA, String[] JB, String[] JC, String ID) {
		String strA = StringUtils.join(JA, ",");
		String strB = StringUtils.join(JB, ",");
		String strC = StringUtils.join(JC, ",");
		if (strC == null) {
			strC = "0";
		}
		String sqltxt = "";
		if (ID != null && !"".equals(ID)) {
			sqltxt = "Update jyh_right_role set r_role='" + NM + "',r_info='" + Inf + "',head_parameters='" + strA
					+ "'," + "left_parameters='" + strB + "',center_parameters='" + strC + "' where ID=" + ID;
		} else {
			sqltxt = "INSERT INTO jyh_right_role (r_role,r_info,head_parameters,left_parameters,center_parameters) "
					+ "VALUES ('" + NM + "','" + Inf + "','" + strA + "','" + strB + "','" + strC + "')";
		}
		String returnStr = jurisdictionDao.getVaultRightRoleIorU(sqltxt);
		return returnStr;
	}

}
